﻿using DynamicViewFormApp.Setting;
using DynamicViewFormApp.Util;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DynamicViewFormApp.Component
{
    class RichTextBoxHandler
    {
        private const int WS_HSCROLL = 0x100000;
        private const int WS_VSCROLL = 0x200000;
        private const int GWL_STYLE = (-16);

        [System.Runtime.InteropServices.DllImport("user32", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
        private static extern int GetWindowLong(IntPtr hwnd, int nIndex);


        internal static void GotFocus(object sender, EventArgs e)
        {
            RichTextBox rtb = sender as RichTextBox;
            //rtb.BorderStyle = BorderStyle.FixedSingle;
            PublicVariable.CurrentRichTextBox = rtb;
        }

        internal static void LostFocus(object sender, EventArgs e)
        {
            RichTextBox rtb = sender as RichTextBox;
            //rtb.BorderStyle = BorderStyle.None;
        }

        internal static void MouseHover(object sender, EventArgs e)
        {
            RichTextBox rtb = sender as RichTextBox;
            rtb.BorderStyle = BorderStyle.FixedSingle;
        }
        internal static void MouseLeave(object sender, EventArgs e)
        {
            RichTextBox rtb = sender as RichTextBox;
            rtb.BorderStyle = BorderStyle.None;
        }

        internal static void BlockContent_Click(object sender, MouseEventArgs e)
        {
            RichTextBox rtb = sender as RichTextBox;
            PublicVariable.CurrentRichTextBox = rtb;
            PublicVariable.RtfContent = rtb.Rtf;
        }

        internal static void RichTextBox_TextChanged(object sender, EventArgs e)
        {
            RichTextBox rtb = sender as RichTextBox;
            PublicVariable.RtfContent = rtb.Rtf;
        }

        internal static void ContentsResized(object sender, ContentsResizedEventArgs e)
        {
            RichTextBox rtb = sender as RichTextBox;
            if (e.NewRectangle.Height >= rtb.Height)
            {
                rtb.Rtf = PublicVariable.RtfContent;
                rtb.Focus();
                rtb.ScrollToCaret();
                MessageBox.Show("当前分页已满，请调整分页内容");
            }
        }

        /// <summary>
        /// 判断是否出现垂直滚动条
        /// </summary>
        /// <param name="ctrl">待测控件</param>
        /// <returns>出现垂直滚动条返回true，否则为false</returns>
        internal static bool IsVerticalScrollBarVisible(Control ctrl)
        {
            if (!ctrl.IsHandleCreated)
                return false;

            return (GetWindowLong(ctrl.Handle, GWL_STYLE) & WS_VSCROLL) != 0;
        }

        /// <summary>
        /// 判断是否出现水平滚动条
        /// </summary>
        /// <param name="ctrl">待测控件</param>
        /// <returns>出现水平滚动条返回true，否则为false</returns>
        internal static bool IsHorizontalScrollBarVisible(Control ctrl)
        {
            if (!ctrl.IsHandleCreated)
                return false;
            return (GetWindowLong(ctrl.Handle, GWL_STYLE) & WS_HSCROLL) != 0;
        }

    }
}
